<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends CI_Controller{
    
    function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url')); 
        $this->load->library(array('form_validation','session'));
        
        
    }
    
    function login(){
        $sessionUser = $this->session->userdata('user');
        if($_SERVER['REQUEST_URI'] !== urlencode('/myproject/index/logout')){
            if(isset($sessionUser) OR $sessionUser !==false){
                if($sessionUser['useradmin'] == 1){
                    redirect(base_url('admin/home/listProfileStaff'));
                }else{
                    if(($sessionUser['username'] !== false) && ($sessionUser['username'] !== null)){
                        redirect(base_url('nhanvien/event/list_event'));
                    }
                }
            }
        }
        $data['returnUrl'] = $this->input->get('returnUrl');
        $data['base_url'] = $this->config->item('base_url');
        $data['js'] = $this->config->item('js');
        $data['css'] = $this->config->item('css');
        $data['error'] = $this->input->get('error');
        $this->load->view("login", $data);
    }
    
    function authen(){
        $returnUrl = $this->input->get('returnUrl');
        $this->form_validation->set_rules('username', 'UserName', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'PassWord', 'required');
        if($this->form_validation->run() == TRUE){
            $user['username'] = $this->input->post('username'); 
            $user['password'] = md5($this->input->post('password'));
            $this->load->model("login_model"); 
            $check = $this->login_model->checkLogin($user['username'], $user['password']); 
            if($check){
                $userdata = array(
                    'username' => $check['UserName'],
                    'useradmin' => $check['TacNhan'], 
                    'MaHoSo' => $check['MaHoSo'],
                    'logged_in' => TRUE );
                $this->session->set_userdata('user', $userdata);
                if($check['TacNhan'] == 1){
                    if(($returnUrl == "")||($returnUrl == false)){
                        $returnUrl = base_url('admin/home/listProfileStaff');
                    }
                    redirect($returnUrl);
                }else{
                    if(($returnUrl == "") || ($returnUrl == false)){
                        $returnUrl = redirect(base_url('nhanvien/event/list_event'));
                    }
                    redirect($returnUrl);
                }
            }else{ 
                $data['base_url'] = $this->config->item('base_url');
                $data['js'] = $this->config->item('js');
                $data['css'] = $this->config->item('css');
                $data['returnUrl'] = $this->input->get('returnUrl');
                $error= urlencode('Sai tên đăng nhập hoặc mật khẩu');
                if($returnUrl === ''){
                    redirect(base_url('/index/login/?error='.$error));
                }else{
                    redirect(base_url('/index/login/?error='.$error.'&returnUrl='.$returnUrl));
                }
            } 
        }
        
         
    }
    
    function logout(){
        echo($_SERVER['REQUEST_URI']);
        $this->session->unset_userdata('user');
        redirect(base_url('/index/login'));
    }
    
}
?>